package com.cino.demos.common.aspect;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.aspectj.lang.JoinPoint;
import org.springframework.stereotype.Component;

@Component
@Aspect
public class LoggingAspect {

    private static final Logger logger = LoggerFactory.getLogger(LoggingAspect.class);

    // 定义切点，这里以所有包下的service包中的方法为例
    @Pointcut("@annotation(com.cino.demos.common.aspect.annotation.AutoLog)")
    public void loggable() {}

    @Around("loggable()")
    public Object logExecutionTime(ProceedingJoinPoint joinPoint) throws Throwable {
        System.out.println("AOP-Around拦截开始");
        long start = System.currentTimeMillis();
        Object proceed = joinPoint.proceed();
        long executionTime = System.currentTimeMillis() - start;
        System.out.println("AOP:"+joinPoint.getSignature() + " executed in " + executionTime + "ms");
        return proceed;
    }
}